package com.ptszyxx.popose.module.base.user;

import android.os.Bundle;

import androidx.lifecycle.ViewModelProviders;

import com.luck.picture.lib.entity.LocalMedia;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.databinding.ActivityUserInfoImproveBinding;
import com.ptszyxx.popose.module.base.user.vm.UserInfoImproveVM;
import com.ysg.base.BaseActivity;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YDialogUtil;
import com.ysg.widget.pic.YPicUtils;

import java.util.ArrayList;

/**
 * 完善用户信息
 */
public class UserInfoImproveActivity extends BaseActivity<ActivityUserInfoImproveBinding, UserInfoImproveVM> {

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_user_info_improve;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public UserInfoImproveVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(UserInfoImproveVM.class);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onPicEvent.observe(this, o -> addAvatar());
        viewModel.uc.onBirthDayEvent.observe(this, o -> showDialogBirthday());
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestOosToken();
    }

    private void addAvatar() {
        YPicUtils.getInstance().addAvatar(UserInfoImproveActivity.this, new YPicUtils.OnResultListener() {
            @Override
            public void onResultListener(ArrayList<LocalMedia> result) {
                if (YCollectionUtil.isNotEmpty(result)) {
                    viewModel.avatarPath.set(result.get(0).getCutPath());
                }
            }
        });
    }

    private void showDialogBirthday() {
        YDialogUtil.getInstance().showDialogDate(this, s -> viewModel.birthday.set(s) );
    }


}